<%@ page language="java" import="java.util.*,java.util.Map.Entry,java.net.URLEncoder" pageEncoding="UTF-8"%>
<%
String jsBasePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";

Object totalcountObj = request.getAttribute("totalcount");
String indexStr = request.getParameter("index");
String sizeStr = request.getParameter("size");
String naviurl = String.valueOf(request.getAttribute("naviurl")) + "?";

// 重新组装查询字符串，除去原有的index与size参数
@SuppressWarnings("unchecked")
Map<String, String[]> map = request.getParameterMap();
for(Map.Entry<String, String[]> entry : map.entrySet()){
	if( ! "index".equals(entry.getKey()) && ! "size".equals(entry.getKey())){
		//naviurl += entry.getKey()+"="+URLEncoder.encode(entry.getValue() [0], "UTF-8") + "&";
		//naviurl += entry.getKey()+"="+entry.getValue() [0] + "&";
		naviurl += entry.getKey()+"="+new String(entry.getValue()[0].getBytes("ISO-8859-1"),"UTF-8") + "&";
	}
}

// 取不到值的情况设定默认值
if(totalcountObj == null) totalcountObj = "0";
if(indexStr == null) indexStr = "1";
if(sizeStr == null) sizeStr = "20";
// 转为数值型
int totalcount = Integer.parseInt(totalcountObj.toString());
int index = available(indexStr);
int size = available(sizeStr);
// 最大分页数
int maxIndex = (totalcount-1)/size+1;

int minShow = 10;

// 计算当前页index的前后页数


%>

<%!
/* 用于输出页码的方法 */
String createIndex(boolean isCurrent, int navIndex, int size, String naviurl){
	if(isCurrent){
		return "<span class='current'>"+navIndex+"</span>";
	}else{
		return "<a href='"+naviurl+"index="+navIndex+"&size="+size+"'>"+navIndex+"</a>";
	}
}

/* 用于合法化页码数字 */
int available(String txtNum){
	int num;
	// 如果字符串不是数字，则返回数字1
	try{
		num = Integer.parseInt(txtNum);
	}catch(Exception exp){
		return 1;
	}
	// 如果数字值不是正整数，则返回数字1
	if(num<=0){
		return 1;
	}
	return num;
}
%>



<div class="pagination">

<!--  “上一页”的逻辑   -->
<%if(index<=1){ %>
<span class="disabled">&lt;&lt; 上一页</span>
<% }else{%>
<a href="<%=naviurl %>index=<%=index-1 %>&size=<%=size %>">&lt;&lt; 上一页</a>
<%} %>





<%
// 如果总页数在minShow条内，则全部显示
if(maxIndex<=minShow) {
	for(int i=0; i<maxIndex; i++){
		out.print(createIndex(i+1==index, i+1, size, naviurl));		 
	} 
}
else
{
// 否则以当前页码index为中心点显示

	// 显示第一个
	if(index>minShow/2){
		out.print("<a href='"+naviurl+"index="+1+"&size="+size+"'>"+1+"...</a>");
	}
	
	// 显示minShow条
	int start = index - minShow/2;
	if(start<0) start = 0;
	if(start>=maxIndex-minShow) start = maxIndex-minShow;
	for(int i=start; i<start+minShow; i++){
		if(i+1>maxIndex) break;
		out.print(createIndex(i+1==index, i+1, size, naviurl));
	}
	
	// 显示最后一个
	if(index<maxIndex-minShow/2){
		out.print("<a href='"+naviurl+"index="+maxIndex+"&size="+size+"'>..."+maxIndex+"</a>");
	}
}
%>




<!--  “下一页”的逻辑   -->
<%if(index>=maxIndex){ %>
<span class="disabled">下一页 &gt;&gt;</span>
<% }else{%>
<a href="<%=naviurl %>index=<%=index+1 %>&size=<%=size %>">下一页 &gt;&gt;</a>
<%} %>
<span>总<%=totalcount %>条</span>
<script type="text/javascript">
	function changePageSize(sender){// 切换分页大小后，让页码变回1，避免分页变大导致超出页数。
		location.href="<%=jsBasePath %><%=naviurl %>index=<%=1 %>&size="+sender.value;
	}
	function goToIndex(event){
		if(event.keyCode==13){
		location.href="<%=jsBasePath %><%=naviurl %>index="+document.getElementById('pagebar_pageindex').value+"&size=<%=size%>";
		}
	}
</script>
<span>每页<span style="border:1px solid #5ca748;overflow: hidden;display: inline-block;margin-bottom: -6px;">
<select onchange="changePageSize(this)" style="margin: -1px;">
	<option value="10"<%=size==10?" selected='selected'":"" %>>10</option>
	<option value="20"<%=size==20?" selected='selected'":"" %>>20</option>
	<option value="50"<%=size==50?" selected='selected'":"" %>>50</option>
	<option value="80"<%=size==80?" selected='selected'":"" %>>80</option>
	<option value="100"<%=size==100?" selected='selected'":"" %>>100</option>
	<option value="200"<%=size==200?" selected='selected'":"" %>>200</option>
	<option value="500"<%=size==500?" selected='selected'":"" %>>500</option>
</select></span>条</span>
<span>第<input id="pagebar_pageindex" type="text" value="<%=index %>"  
onkeydown="goToIndex(event)" title="输入页数按回车键确定" 
style="border:1px solid #5ca748;max-width: 20px;padding:2px 3px;color:#5ca748;font-size: 11px;">页</span>
</div>